package com.useeinfo.asg.modules.security.entity;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.framework.extend.entity.DataEntity;
import com.useeinfo.framework.sugar.tools.CommonSugar;
import net.sf.json.JSONObject;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

/**
 */
@Entity
@Table(name = "system_authority")
public class SystemAuthority extends DataEntity<SystemAuthority> {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;					// 主键编号

	@Temporal(TemporalType.TIMESTAMP)
	private Date createTime;			// 创建记录时间

	@Column(columnDefinition = "bit default 0")
	private boolean deleteStatus;		// 逻辑删除状态    0.否    1.是
	private String authorityName;
	private String authorityDescription;
	@ManyToMany(mappedBy = "authoritySet")
	private Set<SystemRole> roleSet;
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "system_authority_2_resource", joinColumns = @JoinColumn(name = "authority_id"), inverseJoinColumns = @JoinColumn(name = "resource_id"))
	private Set<SystemResource> resourceSet;
	private boolean systemManager;
	private boolean enabled;

	@Override
	public JSONObject toJSONObject() {
		JSONObject jsonObject = new JSONObject();

		jsonObject.put("authorityId", this.getId());
		jsonObject.put("createTime", CommUtil.formatLongDate(this.getCreateTime()));
		jsonObject.put("authorityName", CommonSugar.getTypedDefault(getAuthorityName(), ""));
		jsonObject.put("authorityDescription", CommonSugar.getTypedDefault(getAuthorityDescription(), ""));
		jsonObject.put("systemManager", isSystemManager());
		jsonObject.put("enabled", isEnabled());

		return jsonObject;
	}

	public SystemAuthority() {
	}

	public SystemAuthority(String authorityName,
	                       String authorityDescription,
	                       Set<SystemRole> roleSet,
	                       Set<SystemResource> resourceSet,
	                       boolean systemManager,
	                       boolean enabled) {

		this.authorityName = authorityName;
		this.authorityDescription = authorityDescription;
		this.roleSet = roleSet;
		this.resourceSet = resourceSet;
		this.systemManager = systemManager;
		this.enabled = enabled;
	}

	@Override
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public boolean isDeleteStatus() {
		return deleteStatus;
	}

	public void setDeleteStatus(boolean deleteStatus) {
		this.deleteStatus = deleteStatus;
	}

	public String getAuthorityName() {
		return authorityName;
	}

	public void setAuthorityName(String authorityName) {
		this.authorityName = authorityName;
	}

	public String getAuthorityDescription() {
		return authorityDescription;
	}

	public void setAuthorityDescription(String authorityDescription) {
		this.authorityDescription = authorityDescription;
	}


	public Set<SystemRole> getRoleSet() {
		return roleSet;
	}

	public void setRoleSet(Set<SystemRole> roleSet) {
		this.roleSet = roleSet;
	}

	public Set<SystemResource> getResourceSet() {
		return resourceSet;
	}

	public void setResourceSet(Set<SystemResource> resourceSet) {
		this.resourceSet = resourceSet;
	}

	public boolean isSystemManager() {
		return systemManager;
	}

	public void setSystemManager(boolean systemManager) {
		this.systemManager = systemManager;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void addResource(SystemResource systemResource) {
		this.resourceSet.add(systemResource);
	}

	public void removeResource(SystemResource systemResource) {
		this.resourceSet.remove(systemResource);
	}
}
